package com.link.base.base.rsasecretkey.controller;


import com.link.base.core.RSAUtils.RSAUtils;
import com.link.base.core.RSAUtils.SecretKeyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author HuangLJ
 * @date 2018/12/6 8:37 PMd
 * rsa 相关
 */

@Controller
@RequestMapping("/link/secretkey")
public class SecretKeyController {

    /**
     * @author HuangLJ
     * @date 2019-07-18 10:16
     * 生成访问本地dmp秘钥
     */
    @RequestMapping(value = "/getsecret", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getSecretKey(@RequestParam(value = "headIdName") String headIdName,
                                            @RequestParam(value = "headId") String headId,
                                            HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map<String, Object> result = new HashMap<>(16);
        try {
            result.put("success", true);
            result.put("result", SecretKeyUtil.currentUserDmpEncode(headIdName, headId));
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * @author HuangLJ
     * @date 2019-07-18 10:16
     * 生成公钥，私钥的方法
     */
    @RequestMapping(value = "/genVerifyKey", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> generatorVerifyKey(HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            Map<String, Object> ret = RSAUtils.genKeyPair();
            result.put("success", true);
            result.put("result", ret);

//            String pub = RSAUtils.getBase64ByPublicKey("我家没有小山羊我家没有小山羊我家没有小山羊我家没有小山羊", (String) ret.get("RSAPublicKey"));
//            String sss = RSAUtils.decryptByPrivateKeyAndBase64(pub,(String) ret.get("RSAPrivateKey"));
//            System.out.println(sss);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

}
